package common

import (
	"fmt"
	"github.com/sirupsen/logrus"
	"os"
	"strings"
	"time"
)

// CustomFormatter 自定义格式化器
type CustomLogFormatter struct{}

// Format 实现logrus.Formatter接口的Format方法，自定义日志格式
func (f *CustomLogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
	timestamp := time.Now().Format("2006/01/02 15:04:05")
	var logLevel string
	// 根据日志级别设置不同的颜色
	switch entry.Level {
	case logrus.DebugLevel:
		logLevel = "\033[36mDEBUG\033[0m" // Cyan
	case logrus.InfoLevel:
		logLevel = "\033[32mINFO\033[0m" // Green
	case logrus.WarnLevel:
		logLevel = "\033[33mWARNING\033[0m" // Yellow
	case logrus.ErrorLevel:
		logLevel = "\033[31mERROR\033[0m" // Red
	case logrus.FatalLevel:
		logLevel = "\033[31mFATAL\033[0m" // Red
	case logrus.PanicLevel:
		logLevel = "\033[31mPANIC\033[0m" // Red
	default:
		logLevel = entry.Level.String() // Default
	}
	msg := fmt.Sprintf("\n[SYS-Log] [%s] [%s]\n%s\n", logLevel, timestamp, entry.Message)
	return []byte(msg), nil
}

//var Logger *logrus.Logger

// SetupLogger 初始化日志组件
func SetupLogger() {
	logrus.SetOutput(os.Stdout)
	logrus.SetFormatter(new(CustomLogFormatter))
	logrus.SetReportCaller(true)
}

type SQLLogFormatter struct {
}

func (s *SQLLogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
	timestamp := time.Now().Format("2006/01/02 15:04:05")
	var logLevel string
	// 根据日志级别设置不同的颜色
	switch entry.Level {
	case logrus.DebugLevel:
		logLevel = "\033[36mDEBUG\033[0m" // Cyan
	case logrus.InfoLevel:
		logLevel = "\033[32mINFO\033[0m" // Green
	case logrus.WarnLevel:
		logLevel = "\033[33mWARNING\033[0m" // Yellow
	case logrus.ErrorLevel:
		logLevel = "\033[31mERROR\033[0m" // Red
	case logrus.FatalLevel:
		logLevel = "\033[31mFATAL\033[0m" // Red
	case logrus.PanicLevel:
		logLevel = "\033[31mPANIC\033[0m" // Red
	default:
		logLevel = entry.Level.String() // Default
	}
	str := strings.Replace(entry.Message, "[SQL]", "\u001B[34m[SQL]\u001B[0m", -1)
	msg := fmt.Sprintf("\n[SYS-Log] [%s] [%s]\n%s\n", logLevel, timestamp, str)
	return []byte(msg), nil
}
